import query from './atom/db/query';
import count from './atom/db/count';
import { IBaseTable } from './atom/db/interfaces';

declare const action_id: string;
declare const session_id: string;
declare const spaceid: string;

interface Message {
	_id: string;
}

interface certifiInfo extends IBaseTable {
	name: string; no: string; staff_name: string; staff_number: string; deptid: string; deptname: string; confirm_date: number; effectivedate: number; staus: string; purchase_right: string; enddate: number; org_name: string; memo: string; accessory_url: string;
}


function format(date: number) {
	if (date === null) {
		return '';
	}
	try {
		const _date = new Date(parseFloat(date + ''));
		if (_date) {
			return _date.getFullYear() + '-' + _format(_date.getMonth() + 1) + '-' + _format(_date.getDate());
		}
		return '';
	} catch (error) {
		return '';
	}
}

function _format(d: number) {
	if (d < 10) {
		return '0' + d;
	} else {
		return d;
	}
}

async function atom(msg: Message) {
	const systemid = '6051';
	const { exec, prepare } = query(action_id, session_id, spaceid, systemid);
	const asc: string[] = [];
	const desc: string[] = [];

	// 证照信息
	prepare('certifi_info', ['name', 'no', 'staff_name', 'staff_number', 'deptid', 'deptname', 'confirm_date', 'effectivedate', 'staus', 'purchase_right', 'enddate', 'org_name', 'memo', 'accessory_url', 'certificate'], { _id: msg._id }, 1, 1);
	const [data] = await exec();

	const item = data[0] as certifiInfo;
	item['confirm_date_str'] = format(item.confirm_date);
	item['effectivedate_str'] = format(item.effectivedate);
	item['enddate_str'] = format(item.enddate);

	const staff_number = item.staff_number;

	// const q = query(action_id, session_id, spaceid, systemid);
	// q.prepare('teach_staff', ['staff_number', ''], { staff_number }, 1, 1);
	// const [staff_data] = await q.exec();
	// if (staff_data.length > 0) {
	// 	item[''] = staff_data[0].'';
	// };

	return item;
}